const router = require('koa-router')()
// 微信云开发token
const {
    getToken,
    Addurl,
    selurl,
    delurl,
    updurl
} = require('../../config/databaseapi')
// 校验token
const {
    showtoken
} = require("../../token/showtoken")
// 操作数据库
const result = require('../../config/result')
// 校验格式问题
const {
    shoinfo
} = require("../../config/register")
// 图片上传
const {
    upload,
    confun
} = require("../../cos/index")
// after 后台服务器
const moment = require('moment')
moment.locale('zh-cn')
router.prefix('/api/after')

// 添加商家信息
router.post('/upspping', new showtoken().m, async (ctx) => {
    const id = Date.now()
    let username = ctx.request.body.data.username
    let address = ctx.request.body.data.address
    let image = ctx.request.body.data.image
    new shoinfo(ctx, username, address, image).startlist()
    let query = `db.collection("setSeller").add({data:{uid:${id},username:'${username}',addres:'${address}',image:'${image}'}})`
    try {
        await new getToken().postlive(Addurl, query)
        new result(ctx, '添加成功', 200).answer()
    } catch (error) {
        console.log("错哪里:", error)
        new result(ctx, '服务器出现错误', 500).answer()
    }

})


// 添加商品
router.post("/addshpping", new showtoken().m, async ctx => {
    const time = moment().format("YYYY-MM-DD HH:mm:ss") //上架时间
    const uid = Date.now()
    const spname = ctx.request.body.Dish_name //商品名称
    const price = ctx.request.body.Dish_price //商品单价
    const types = ctx.request.body.Dish_types //商品类型 份 瓶
    const image = ctx.request.body.fileList[0].url //商品图片
    const classification = ctx.request.body.Dish_classification //商品类型 '牛排' '小吃' '饮料' '披萨'
    const flavor = ctx.request.body.Dish_flavor //商品口味
    const Monthly_sales = 0 //销售
    const operation = true //是否上架 true false
    const obj = {
        cretime: time,
        uid,
        spname,
        price,
        types,
        image,
        classification,
        flavor,
        Monthly_sales,
        operation
    }
    const data = JSON.stringify(obj)
    const query = `db.collection('shoplist').add({data:${data}})`
    const squery = `db.collection('shoplist').where({spname:'${spname}'}).get()`
    const fonum = `db.collection('food-type').where({label:'${JSON.stringify(classification)}'}).update({data:{fonum:db.command.inc(1)}})`
    try {
        const user = await new getToken().postlive(selurl, squery)
        if (user.data.length > 0) {
            new result(ctx, '该商品已经存在', 200).answer()
        } else {
            if (!spname || !price || !types || !image || !classification || !flavor) {
                new result(ctx, "商品添加失败!", 401).answer()
            } else {
                try {
                    if (!data) {
                        new result(ctx, "商品添加有误!", 401).answer()
                    } else {
                        await new getToken().postlive(Addurl, query)
                        await new getToken().postlive(updurl, fonum)
                        new result(ctx, '添加成功', 200).answer()
                    }
                } catch (error) {
                    new result(ctx, '服务器出现错误', 500).answer()
                }
            }
        }
    } catch (error) {
        new result(ctx, '服务器出现错误', 500).answer()
    }

})


// 修改前查询内容
router.post('/showlistID', async ctx => {
    console.log("查询的值:", ctx.request.body)
    const id = ctx.request.body.id
    const query = `db.collection("shoplist").where({_id:'${id}'}).get()`
    try {
        const user = await new getToken().postlive(selurl, query)
        if (user.data.length > 0) {
            const data = user.data.map(item => {
                return JSON.parse(item)
            })
            new result(ctx, 'success', 200, data).answer()
        }
    } catch (error) {
        new result(ctx, "删除失败,服务器出现错误", 500).answer()
    }
})

// 查询后修改数据内容
router.post("/setlistID", async ctx => {
    const time = moment().format("YYYY-MM-DD HH:mm:ss") //上架时间
    const spname = ctx.request.body.Dish_name //商品名称
    const price = ctx.request.body.Dish_price //商品单价
    const types = ctx.request.body.Dish_types //商品类型 份 瓶
    const image = ctx.request.body.fileList[0].url //商品图片
    const classification = ctx.request.body.Dish_classification //商品类型 '牛排' '小吃' '饮料' '披萨'
    const flavor = ctx.request.body.Dish_flavor //商品口味
    const Monthly_sales = 0 //销售
    const operation = true //是否上架 true false
    const obj = {
        cretime: time,
        spname,
        price,
        types,
        image,
        classification,
        flavor,
        Monthly_sales,
        operation
    }
    const data = JSON.stringify(obj)
    const query = `db.collection("shoplist").update({data:${data}})`
    try {
        const user = await new getToken().postlive(updurl, query)
        console.log("修改了吗", user)
        // const datas = user.data.map(item => {
        //     return JSON.parse(item)
        // })
        // new result(ctx, '修改成功', datas, 200)
    } catch (error) {
        console.log("哪里错了", error)
        // new result(ctx, '修改错误', 500).answer()
    }
})

router.post("/oagement", new showtoken().m, async ctx => {
    let data = ctx.request.body
    console.log("未知内容:", data)
    let query = `db.collection('oagement').add({data:${data}})`
})



//修改商品内容
router.post('/setspping', new showtoken().m, async ctx => {
    console.log("传过来的值")
    ctx.body = {
        title: 'sucess'
    }
})



module.exports = router